package com.fj.netty.eventloop;

import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.ScheduledFuture;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author fj
 * @date 2024/1/2 14:22
 */
public class TestEventLoop {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(3);
        DefaultEventLoopGroup eventExecutors = new DefaultEventLoopGroup();
        System.out.println(nioEventLoopGroup.next());
        System.out.println(nioEventLoopGroup.next());
        System.out.println(nioEventLoopGroup.next());
        System.out.println(nioEventLoopGroup.next());
        //普通任务
//        nioEventLoopGroup.next().execute(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(5);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("sub thread");
//        });
//        System.out.println("main thread");
        //延时任务
        ScheduledFuture<Integer> result = nioEventLoopGroup.next().schedule(() -> {
            System.out.println("延迟计算");
            return 6;
        }, 2, TimeUnit.SECONDS);
        System.out.println(result.get());
        //循环任务
//        nioEventLoopGroup.next().scheduleAtFixedRate(()-> System.out.println("ok"),0,1,TimeUnit.SECONDS);
//        nioEventLoopGroup.next().scheduleWithFixedDelay(() -> System.out.println("延迟执行"), 5, 3, TimeUnit.SECONDS);
    }
}
